Imports System.ComponentModel
Imports PostSharp
Imports Liftoff.Samples.BusinessObjects

Namespace PostSharp.Samples.Binding
  Friend Class Program
        Public Shared Sub Main()
            Dim customer As New Customer()

            'Get two connections where the UI can easily plug in
            'You can use one of the next tree lines to get out the interface for property changed events.
            'notificationSource = Post.Cast(Of Customer, INotifyPropertyChanged)(customer)
            'notificationsource = DirectCast(customer, INotifyPropertyChanged)
            'notificationSource = customer
            Dim editor As IEditableObject = DirectCast(customer, IEditableObject)
            Dim notificationSource As INotifyPropertyChanged = DirectCast(customer, INotifyPropertyChanged)

            ' Now that we have INotifyPropertyChanged, we can register our PropertyChanged event handler.
            Console.WriteLine("We register an event handler for the PropertyChanged event," + " so we will be notified when a property will change.")
            AddHandler notificationSource.PropertyChanged, AddressOf Program_PropertyChanged

            Console.WriteLine("We initialize the customer name to 'Sylvestre' when the object is not in editable mode. " + " Changes are 'committed' automatically.")

            ' We update the object outside a transaction. The change is accepted automatically.
            customer.FirstName = "Sylvestre"

            Console.WriteLine("Now we enter edit mode and we change the name to 'John' " + "and the turnover to 1500.")

            ' Start editing. Note the post-compilation cast again.
            'Post.Cast(Of Customer, IEditableObject)(customer).BeginEdit()
            editor.BeginEdit()

            ' This should cause the PropertyChanged to be raised.
            customer.FirstName = "John"
            customer.Turnover = 1500
            'This time, the PropertyChangedEvent should NOT fire, its very importend for a performant UI.
            Console.WriteLine("Setting the turnover to 1500 again, and the 'PropertyChanged' event should NOT fire.")
            customer.Turnover = 1500

            ' Cancel changes.
            Console.WriteLine("")
            Console.WriteLine("We cancel ...")
            'Post.Cast(Of Customer, IEditableObject)(customer).CancelEdit()
            editor.CancelEdit()

            Console.WriteLine("After cancel, customer.FirstName = {{{0}}}, customer.Turnover = {{{1}}}.", customer.FirstName, customer.Turnover)

            Console.WriteLine("Please hit <Enter> for exit.")
            Console.Read()

            'Dont forget cleanup.
            RemoveHandler notificationSource.PropertyChanged, AddressOf Program_PropertyChanged
        End Sub

    ' Called when the PropertyChanged is invoked on the Customer entity.
        Private Shared Sub Program_PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
            If String.IsNullOrEmpty(e.PropertyName) Then
                Console.WriteLine("PropertyChanged event fired without a property name. BindingSource objects usually refresh all  props.")
            Else
                Console.WriteLine("PropertyChanged event fired for property  {0}.", e.PropertyName)
            End If
        End Sub
  End Class
End Namespace

